#coding:utf-8
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
import json

import common.httpclient as http
import common.tips as tips
import common.modelAdminUser as modelAdminUser
import config.myurls as myurls
import common.function as myfc
import common.category as mycategory

def list(req):
  '新浪接口'
  
  main_nav=[]
  main_nav.append({"url":"","name":"新浪接口"})
  context={}
  context['title'] = '-新浪接口'
  context['main_nav']=main_nav
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetSinapayapiList;
  
  param=[];
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['list']=rt['list']
  return render(req, 'sinapayapi_list.html', context)

def edit(req):
  '新浪接口编辑'
  
  id=0
  try:
    id=int(str(req.GET.get("id")))
  except ValueError:
    pass
  
  if id==0:
    return tips.error(req,"id不能为0","")
  
  main_nav=[]
  main_nav.append({"url":"/sinapayapi/","name":"银行限额列表"})
  main_nav.append({"url":"","name":"新浪接口编辑"})
  context={}
  context['title'] = '-新浪接口编辑'
  context['main_nav']=main_nav
  context['whether_or_not']=mycategory.getCategoryByName("whether_or_not",req)
  
  url=myurls.ApiGetSinapayapiInfo;
  
  param=[];
  param.append(("id",id))
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['info']=json.loads(rt['info'])
  return render(req, 'sinapayapi_edit.html', context)


def editpost(req):
  '提交编辑'
  
  rt={}
  rt['response_code']=0
  rt['response_message']="待提交"
  
  param=[]
  
  for key in req.POST:
    param.append((key,req.POST.get(key)))
  
  
  url=myurls.ApiUpdSinapayapiInfo;
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
    
  return tips.returnAjax(rt)
  #return HttpResponse(json.dumps(rt),content_type="application/json")
  #return tips.success(req,"提交成功","/sinapayapi/edit/?id="+req.POST.get("id"))